﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Comdiv.QWeb.ViewEngine;
using NUnit.Framework;

namespace Comdiv.QWeb.Test.ViewEngine
{
    [TestFixture]
    public class basic_process_test
    {
        public class testdata {
            public int val;
        	public int x2;
        }
        [Action("bpt.test")]
        public class testaction:IAction {
            [Bind] private int retval;
        	[Bind(Name = "x0")] private int x;
            public object Process(QWebContext context) {
                return new testdata {val = retval, x2 = x };
            }
        }
        public class testview : QViewBase {
            [QViewBind] private int val;

        	[QViewBind(Name = "x2")] private int x;
        	
            [ViewData] private testdata item;
            protected override void render() {
                outp("<div>val:");
                outp(val);
                outp("</div><div>itemtype:");
                outp(item.GetType().Name);
                outp("</div>");
				outp("<div>"+x+"</div>");
            }
        }
        [Test]
        public void test_manual_seted_view() {
            var reg = new QWebServiceRegistry();
            reg.QViewFactory.SetView("/bpt/test",new QViewDescriptor{Name = "/bpt/test",NeedRecompile = false,Type=typeof(testview)});
            var h = new QWebHandler(reg);
            var sw = new StringWriter();
            var ctx = new QWebContext("http://localhost/bpt/test.qview.qweb").Set("retval", 45).Set("x0",23);
            ctx.Output = sw;
            h.ProcessRequest(ctx);
            Console.WriteLine(sw.ToString());
            Assert.AreEqual("<div>val:45</div><div>itemtype:testdata</div><div>23</div>",sw.ToString());
        }
    }
}
